﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using monitor_2_DBwork;

namespace monitor_server_2_WPF.DeviceListParamPanels
{
    /// <summary>
    /// Логика взаимодействия для AddDevicePanel.xaml
    /// </summary>
    public partial class AddDevicePanel
    {

        private static readonly DeviceListTabControl.UpdateRegionTreeNodeDelegate UpdateRegionTreeNode = DeviceListTabControl.UpdateRegion;
        private static string _station;

        private AddDevicePanel()
        {
            InitializeComponent();
            AcceptButton.Content = new MyDockPanel(Res.GetBitMapFromPng("accept_image_16"), Res.Rm.GetString("Add"));
            DeviceLabel.Content = Res.Rm.GetString("Name");
            IpLabel.Content = Res.Rm.GetString("IPAddress");
            _infos.Add(InfoType.EmptyName);
            _infos.Add(InfoType.InvalidIp);
        }

        public static UIElement GetPanel(string station)
        {
            var panel = new AddDevicePanel();
            panel.DeviceBox.ItemsSource = DBwork.GetDeviceList();

            _station = station;
            return panel;
        }

        private void AcceptButton_Click(object sender, RoutedEventArgs e)
        {
            if (DeviceBox.SelectedItem == null)
                AddInfo(InfoType.EmptyName);
            if (!Res.IpCheck(IpBox.Text))
                AddInfo(InfoType.InvalidIp);
            if (_infos.Count > 0)
                return;
            if (!DBwork.AddDevice(DeviceBox.SelectedItem.ToString(), _station, IpBox.Text))
            {
                MessageBox.Show(Res.Rm.GetString("Error"));
            }
            else
            {
                MessageBox.Show(Res.Rm.GetString("Complete"));
                UpdateRegionTreeNode();
            }
        }

        private void AddInfo(InfoType type)
        {
            if (AcceptButton.IsEnabled)
                AcceptButton.IsEnabled = false;
            if (_infos.Contains(type)) return;
            _infos.Add(type);
            switch (type)
            {
                case InfoType.EmptyName:
                    ShowInfoBox(EmptyNameInfoBox);
                    break;
                case InfoType.InvalidIp:
                    ShowInfoBox(InvalidIpInfoBox);
                    break;
            }
        }

        private void RemoveInfo(InfoType type)
        {

            if (!_infos.Contains(type)) return;
            _infos.Remove(type);
            if (_infos.Count < 1)
                AcceptButton.IsEnabled = true;
            switch (type)
            {
                case InfoType.EmptyName:
                    HideInfoBox(EmptyNameInfoBox);
                    break;
                case InfoType.InvalidIp:
                    HideInfoBox(InvalidIpInfoBox);
                    break;
            }
        }

        private void ShowInfoBox(TextBlock tb)
        {
            tb.Visibility = Visibility.Visible;
            var a = new DoubleAnimation(0.0, 1.0, new Duration(new TimeSpan(0, 0, 0, 1)));
            tb.BeginAnimation(OpacityProperty, a);
        }

        private void HideInfoBox(TextBlock tb)
        {
            if(ContentPanel.VerticalAlignment == VerticalAlignment.Bottom)
                ContentPanel.VerticalAlignment = VerticalAlignment.Top;
            var a = new DoubleAnimation(1.0, 0.0, new Duration(new TimeSpan(0, 0, 0, 0, 500)));
            tb.BeginAnimation(OpacityProperty, a);
            tb.Visibility = Visibility.Collapsed;
        }

        private readonly List<InfoType> _infos = new List<InfoType>(3);

        enum InfoType
        {
            EmptyName,
            InvalidIp
        }

        private void DeviceBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RemoveInfo(InfoType.EmptyName);
        }

        private void IpBox_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            if(Res.IpCheck(IpBox.Text))
                RemoveInfo(InfoType.InvalidIp);
            else
            {
                AddInfo(InfoType.InvalidIp);
            }
        }
    }
}
